package org.modbusPorject.holder;

import com.intelligt.modbus.jlibmodbus.data.DataHolder;
import com.intelligt.modbus.jlibmodbus.data.ModbusCoils;
import com.intelligt.modbus.jlibmodbus.exception.IllegalDataAddressException;
import com.intelligt.modbus.jlibmodbus.exception.IllegalDataValueException;
import org.modbusPorject.service.ModbusEventListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: study
 * @BelongsPackage: org.modbusPorject.holder
 * @Author: zhangzhanheng
 * @CreateTime: 2022-09-26  10:07
 * @Description: TODO
 * @Version: 1.0
 */
public class MyOwnDataHolder  extends DataHolder {
    final List<ModbusEventListener> modbusEventListenerList = new ArrayList<ModbusEventListener>();

    public MyOwnDataHolder() {
        // you can place the initialization code here
        /*
         * something like that: setHoldingRegisters(new
         * SimpleHoldingRegisters(10)); setCoils(new Coils(128)); ... etc.
         */

        setCoils(new ModbusCoils(128));

    }

    public void addEventListener(ModbusEventListener listener) {
        modbusEventListenerList.add(listener);
    }

    public boolean removeEventListener(ModbusEventListener listener) {
        return modbusEventListenerList.remove(listener);
    }

    @Override
    public int[] readHoldingRegisterRange(int offset, int quantity) throws IllegalDataAddressException {
        for (ModbusEventListener l : modbusEventListenerList) {
            l.readHoldingRegisterRange(offset, quantity);
        }
        return super.readHoldingRegisterRange(offset, quantity);
    }

    @Override
    public int[] readInputRegisterRange(int offset, int quantity) throws IllegalDataAddressException{
        for (ModbusEventListener l : modbusEventListenerList) {
            l.readInputRegisterRange(offset, quantity);
        }
        return super.readInputRegisterRange(offset, quantity);
    }

    @Override
    public void writeHoldingRegister(int offset, int value) throws IllegalDataAddressException,
            IllegalDataValueException {
        for (ModbusEventListener l : modbusEventListenerList) {
            l.onWriteToSingleHoldingRegister(offset, value);
        }
        super.writeHoldingRegister(offset, value);
    }

    @Override
    public void writeHoldingRegisterRange(int offset, int[] range)
            throws IllegalDataAddressException, IllegalDataValueException {
        for (ModbusEventListener l : modbusEventListenerList) {
            l.onWriteToMultipleHoldingRegisters(offset, range.length, range);
        }
        super.writeHoldingRegisterRange(offset, range);
    }

    @Override
    public void writeCoil(int offset, boolean value) throws IllegalDataAddressException,
            IllegalDataValueException {
        for (ModbusEventListener l : modbusEventListenerList) {
            l.onWriteToSingleCoil(offset, value);
        }
        super.writeCoil(offset, value);
    }

    @Override
    public void writeCoilRange(int offset, boolean[] range) throws IllegalDataAddressException,
            IllegalDataValueException {
        for (ModbusEventListener l : modbusEventListenerList) {
            l.onWriteToMultipleCoils(offset, range.length, range);
        }
        super.writeCoilRange(offset, range);
    }
}
